package com.anche.cywebnew.Utils.Encryption;

import authNew.auth;
import com.anche.cywebnew.Pojo.systemManager.CyTdInfo;
import com.anche.cywebnew.Pojo.systemManager.SysOrganization;
import sqm.AuthCode;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class AuthorizeUtil {

    /**
     * 对查验机构进行授权码校验
     *
     * @return
     */
    public boolean doAuthorize(SysOrganization sysOrganization) {
        boolean isPass = false;
        if (sysOrganization.getSqfs() != null && sysOrganization.getSqfs().equals("1")) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String currDate = dateFormat.format(new Date());
            Map<String, String> map = auth.checkCode(sysOrganization.getCyqxh(), "00", currDate, sysOrganization.getSqm());
            if (map.get("code").equals("1")) {
                isPass = true;
            }
        } else {
            AuthCode authCode = new AuthCode();
            String cipherText = authCode.Encode(sysOrganization.getCyqxh());
            if (sysOrganization.getSqm().equals(cipherText)) {
                isPass = true;
            }
        }
        return isPass;
    }

    /**
     * 对查验区通道进行授权码校验
     *
     * @return
     */
    public boolean doAuthorize(CyTdInfo cyTdInfo) {
        boolean isPass = false;
        if (cyTdInfo.getSqfs() != null && cyTdInfo.getSqfs().equals("1")) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String currDate = dateFormat.format(new Date());
            String cyqtd = Integer.parseInt(cyTdInfo.getCyqtd()) > 9 ? cyTdInfo.getCyqtd() : "0" + cyTdInfo.getCyqtd();
            Map<String, String> map = auth.checkCode(cyTdInfo.getCyqxh(), cyqtd, currDate, cyTdInfo.getSqm());
            if (map.get("code").equals("1")) {
                isPass = true;
            }
        } else {
            AuthCode authCode = new AuthCode();
            String cipherText = authCode.Encode(cyTdInfo.getCyqxh() + cyTdInfo.getCyqtd());
            if (cyTdInfo.getSqm().equals(cipherText)) {
                isPass = true;
            }
        }
        return isPass;
    }


    public static void main(String[] args) {
        AuthorizeUtil authorizeUtil = new AuthorizeUtil();
        String organs = "";
        List<SysOrganization> sysOrganizations = new ArrayList<>();


        String[] split = organs.split(",");
        for (String s : split) {
            SysOrganization sysOrganization = new SysOrganization();
            sysOrganization.setCyqxh(s);
            sysOrganization.setSqfs("0");
            sysOrganizations.add(sysOrganization);
        }

        for (SysOrganization sysOrganization : sysOrganizations) {
            authorizeUtil.doAuthorize(sysOrganization);
        }


    }

}
